package com.tajiang.business.setting.fragment;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.businesscount.dialog.StoreOpenTimeDialog;
import com.tajiang.business.common.application.TJApp;
import com.tajiang.business.common.fragment.BaseFragment;
import com.tajiang.business.common.utils.PostDataTools;
import com.tajiang.business.common.utils.SharedPreferencesUtils;
import com.tajiang.business.common.utils.ToastUtils;
import com.tajiang.business.common.utils.UserUtils;
import com.tajiang.business.login.activity.LoginActivity;
import com.tajiang.business.setting.message.MessageMenuActivity;
import com.tajiang.business.setting.print.PrintSettingActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置
 */
public class MySettingFragment extends BaseFragment {

    @BindView(R.id.my_fragment_no)
    TextView tvNo;

    @BindView(R.id.my_fragment_status)
    TextView tvStatus;

    @BindView(R.id.my_fragment_status_img)
    ImageView imgStatus;

    @BindView(R.id.my_fragment_noice)
    ImageView imgNoice;

    @Override
    protected void initTopBar() {

        setTitle("设置");
        setLeftByImgId(R.drawable.set_up_news);
        setOnLeftClick(new OnLeftClick() {

            @Override
            public void leftClick() {

                Intent intent = new Intent(getActivity(), MessageMenuActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_my_setting);
    }

    @Override
    protected void initData() {

        PostDataTools.shop_info(getActivity(), new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                tvNo.setText(UserUtils.mBusinessAccountInfo.getPhone());
                initStoreState();
            }
        });

        if(TJApp.new_order_noice == false){
            imgNoice.setBackgroundResource(R.drawable.set_up_turn_off);
        }else{
            imgNoice.setBackgroundResource(R.drawable.set_up_open);
        }

    }

    public void onResume(){
        super.onResume();

        initStoreState();
    }

    private void initStoreState(){

        if(UserUtils.mBusinessAccountInfo.getBussState() == 0){
            tvStatus.setText("暂停营业");
            imgStatus.setBackgroundResource(R.drawable.set_up_turn_off);
        }else{
            tvStatus.setText("营业中");
            imgStatus.setBackgroundResource(R.drawable.set_up_open);
        }
    }

    @OnClick(R.id.my_fragment_layout_print)
    public void print(){

        Intent intent = new Intent(getActivity(), PrintSettingActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.my_fragment_status_img)
    public void store_status(){

        if(UserUtils.mBusinessAccountInfo.getBussState() == 1){

            StoreOpenTimeDialog.showDialog(getActivity(), new StoreOpenTimeDialog.DialogCallback() {

                @Override
                public void callback(int position, String hour, String minute) {

                    if(position == 0){
                        changeStoreState(0, hour + ":" + minute);
                    }else if(position == 1){
                        changeStoreState(0, "");
                    }
                }
            });
        }else{
            changeStoreState(1, "");//开始营业
        }
    }

    @OnClick(R.id.my_fragment_noice)
    public void noice(){

        if(TJApp.new_order_noice == false){
            ToastUtils.showShort("已开启新订单语音提示");
            TJApp.new_order_noice = true;
            imgNoice.setBackgroundResource(R.drawable.set_up_open);
            SharedPreferencesUtils.put(SharedPreferencesUtils.AUDIO_ATTENTION, true);
        }else{
            ToastUtils.showShort("已关闭新订单语音提示");
            TJApp.new_order_noice = false;
            imgNoice.setBackgroundResource(R.drawable.set_up_turn_off);
            SharedPreferencesUtils.put(SharedPreferencesUtils.AUDIO_ATTENTION, false);
        }
    }

    @OnClick(R.id.my_fragment_commit)
    public void commit(){

        PostDataTools.user_logout(getActivity(), new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                ToastUtils.showShort(message);
            }
        });

        SharedPreferencesUtils.remove(SharedPreferencesUtils.LOGIN_NAME);
        SharedPreferencesUtils.remove(SharedPreferencesUtils.LOGIN_PWD);

        Intent intent = new Intent(getActivity(), LoginActivity.class);
        getActivity().startActivity(intent);
    }

    /**
     * @param state 商家调整营业状态
     * @param nextTime 下一段开启时间
     */
    public void changeStoreState(int state, String nextTime){

        PostDataTools.shop_state(getActivity(), state, nextTime, new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                initStoreState();
            }
        });
    }
}
